<template>
  <div class="dialog" v-show="visible">
    <div class="dialog-wrap">
      <div class="dialog-header">
        <slot name="header" />
        <button
          class="dialog-header-close"
          @click="$emit('update:visible', false)"
          v-on="$listeners"
        >
          X
        </button>
      </div>
      <div class="dialog-body">
        <slot></slot>
      </div>
      <div class="dialog-footer">
        <slot name="footer"></slot>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "Dialog",
  props: {
    type: Boolean,
    required: true,
    visible:false
  },
  watch: {
    visible:{
      immediate:true, // 开启立即监听
      handler(){
        if(this.visible){
          document.documentElement.style.overflow = 'hidden'
        }else {
          document.documentElement.style.overflow = 'auto'
        }
      }
    }
  }
};
</script>

<style lang="less" scoped>
.dialog {
  // 最外层
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.3);

  // 内层水平垂直居中
  .dialog-wrap {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 460px;
    height: 230px;
    box-sizing: border-box; // 内减模式
    padding: 12px;
    transform: translate(-50%, -50%);
    background-color: #fff;
    .dialog-header {
      display: flex;
      justify-content: space-between;
      margin: 10px;
      font-size: 18px;
      button {
        width: 40px;
        background-color: red;
        border: none;
        border-radius: 5px;
        color: #fff;
      }
    }
    .dialog-body{
      font-size: 20px;
      text-align: center;
      height: 100px;
      line-height: 100px;
      color: rgb(19, 19, 16);
      text-shadow: 1px 2px 1px grey;
    }
  }
}
</style>
